﻿#include "CmdParamHandler.h"
#include "define.h"
#include "Utils.h"

CmdParamHandler::CmdParamHandler(int argc,char** args,vector<string> &optParas,int maxPara)
{
	vector<CmdParameter> vecPara(argc - 1);
	vector<CmdParameter>::iterator pi = vecPara.begin();
	for(int i=1;i!=argc;i++){
		if(find(optParas.begin(),optParas.end(),string(args[i]))!=optParas.end()){
			vecPara[i-1] = (CmdParameter(string(args[i]),CmdParameter::PRIORITY_OPTIONAL));
		}else{
			vecPara[i-1] = (CmdParameter(string(args[i]),i));
		}

	}
	sort(vecPara.begin(),vecPara.end());
	pi = vecPara.begin();
	while(pi!=vecPara.end()){
		if(pi->priority==CmdParameter::PRIORITY_OPTIONAL)
			break;
		pi++;
	}
	optBeg = pi - vecPara.begin();
	unique(pi,vecPara.end());
	parameterList = vecPara;
	maxParas = maxPara;
	maxPrios = maxPara - optParas.size();
	isParasLegal = checkLegal();
#ifdef DEBUG
	cout << "************DEBUG INFO:SORTED ARGS*****************"<< endl;
	showParams();
	cout << "************DEBUG INFO:SORTED ARGS*****************"<< endl;
#ifdef WINDOWS
	system("pause");
#else
	string inp;
	cin >>inp;
#endif
#endif
}

bool CmdParamHandler::checkLegal(){
	int paraNum = parameterList.size();
	if(paraNum > maxParas)
		return false;
	else if(maxPrios != optBeg)
		return false;
	else
		return true;
}

string CmdParamHandler::paraPrioX(int x){
	if(x <= parameterList.size()&& x > 0){
		return parameterList[x-1].param;
	}else{
		return "ERR_PARAM";
	}
}

bool CmdParamHandler::hasThisOptPara(const string &str){
	vector<CmdParameter>::iterator i;
	i =  parameterList.begin() + optBeg;
#ifdef DEBUG
	cout << "************DEBUG INFO:OPTS PARAS*****************"<< endl;
#endif
	while(i!=parameterList.end()){
#ifdef DEBUG
		cout << "param:" << i->param << " str:" << str << endl;
#endif
		if(i->param==str)return true;
		i++;
	}
#ifdef DEBUG
	cout << "************DEBUG INFO:OPTS PARAS*****************"<< endl;
	system("pause");
#endif
	return false;
}

void CmdParamHandler::showParams(){
	vector<CmdParameter>::iterator i = parameterList.begin();
	cout << "======beginning of Params======" <<endl;
	while(i!=parameterList.end())
		cout<<*i++<<endl;
	cout << "=========end of Params=========" <<endl;
}

void CmdParamHandler::showExistOpt(){
	vector<CmdParameter>::iterator i = parameterList.begin() + optBeg;
	cout << "======beginning of Opts======" <<endl;
	while(i!=parameterList.end())
		cout<<*i++<<endl;
	cout << "=========end of Opts=========" <<endl;
}

#ifdef MAIN_CLASS_PARAH
int main(int argc,char** args){
#else
int main_debug(int argc,char** args){
#endif
	vector<string> ops(3);
	ops[0] = "y";
	ops[1] = "a";
	ops[2] = "c";
	CmdParamHandler cph(argc,args,ops,6);
#ifdef DEBUG
	cph.showParams();
	cph.showExistOpt();
#ifdef WINDOWS
	system("pause");
#else
	string inp;
	cin >>inp;
#endif
#endif
	string in;
	cin >> in;
	for(int i = 1 ; i!= 10 ;i++){
		cout << cph.paraPrioX(i) << endl;
	}

#ifdef WINDOWS
	system("pause");
#else
#endif
	return 1;
}
